package com.situ.company.web220111.project.dao.impl;

import com.situ.company.web220111.project.dao.IProjectDao;
import com.situ.company.web220111.project.model.ProjectModel;
import com.situ.company.web220111.util.FmtEmpty;
import com.situ.company.web220111.util.JDBCUtil;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

public class ProjectDaoImpl implements IProjectDao {

    private static String table = " project ";
    private static String cols = " code,name,start_time,end_time ";

    @Override
    public String insert(ProjectModel model) {//ok
        String sql = String.format("insert into %s(%s) values(?,?,?,?)", table, cols);
        return JDBCUtil.update(sql, model.getCode(), model.getName(), model.getStartTime(), model.getEndTime());
    }

    @Override
    public String deleteByCode(ProjectModel model) {
        String sql = String.format("delete from %s where code=? ", table);
        return JDBCUtil.update(sql, model.getCode());
    }

    @Override
    public String delete(ProjectModel model) {
        StringBuffer sql = new StringBuffer("delete from ");
        sql.append(table);
        List<Object> values = appendWhere(sql, model);
        return JDBCUtil.update(sql.toString(), values);
    }

    @Override
    public String updateAll(ProjectModel model) {//ok
        String sql = String.format("update %s set name=?,start_time=?,end_time=?  where code=? ", table);
        return JDBCUtil.update(sql, model.getName(), model.getStartTime(), model.getEndTime(), model.getCode());
    }

    @Override
    public String updateActive(ProjectModel model) { //-------------------------------------时间
        StringBuffer sql = new StringBuffer("update ").append(table).append(" set id=id ");
        List<Object> values = appendSet(sql, model);
        return JDBCUtil.update(sql.toString(), values);
    }

    @Override
    public List<ProjectModel> selectList(ProjectModel model) {//ok
        StringBuffer sql = new StringBuffer("select id, ").append(cols).append(" from ").append(table);
        List<Object> values = appendWhere(sql, model);
        Map<String, String> map = new HashMap<>();
        map.put("code", "code");
        map.put("name", "name");
        map.put("startTime", "start_time");
        map.put("endTime", "end_time");
        return JDBCUtil.querylist(sql.toString(), values, ProjectModel.class, map);
    }

    @Override
    public ProjectModel selectModel(ProjectModel model) {//ok
        StringBuffer sql = new StringBuffer("select id, ").append(cols).append(" from ").append(table);
        List<Object> values = appendWhere(sql, model);
        Map<String, String> map = new HashMap<>();
        map.put("code", "code");
        map.put("name", "name");
        map.put("startTime", "start_time");
        map.put("endTime", "end_time");
        return JDBCUtil.queryModel(sql.toString(), values, ProjectModel.class, map);
    }


    /**
     * 拼接sql的where语句
     *
     * @param sql
     * @param model
     * @return
     */
    private List<Object> appendWhere(StringBuffer sql, ProjectModel model) { //***正确理解isEmpty
        sql.append(" where 1=1 ");
        List<Object> values = new ArrayList<>();
        String code = model.getCode();
        if (!FmtEmpty.isEmpty(code)) {
            sql.append(" and code like  ? ");
            values.add(code);
        }
        String name = model.getName();
        if (!FmtEmpty.isEmpty(name)) {
            sql.append(" and name like  ? ");
            values.add(name);
        }
        String startTime = model.getStartTime();
        if (!FmtEmpty.isEmpty(startTime)) {
            sql.append(" and start_time = ? ");
            values.add(startTime);
        }
        String endTime = model.getEndTime();
        if (!FmtEmpty.isEmpty(endTime)) {
            sql.append(" and end_time = ? ");
            values.add(endTime);
        }
        if (model.isPageOn()) {
            sql.append(" limit ?,?");
            values.add(model.getRowStart());
            values.add(model.getRowCount());
        }
        return values;
    }

    /**
     * 拼接sql的修改set语句
     *
     * @param sql
     * @param model
     * @return
     */
    private List<Object> appendSet(StringBuffer sql, ProjectModel model) {
        List<Object> values = new ArrayList<>();
        String name = model.getName();
        if (name != null) {
            sql.append(" , name = ? ");
            values.add(name);
        }
        String startTime = model.getStartTime();
        if (startTime != null) {
            sql.append(" , start_time = ? ");
            values.add(startTime);
        }
        String endTime = model.getEndTime();
        if (endTime != null) {
            sql.append(" , end_time = ? ");
            values.add(endTime);
        }
        sql.append(" where code = ? ");
        values.add(model.getCode());
        return values;
    }

    @Override
    public Integer selectCount(ProjectModel model) {
        StringBuffer sql = new StringBuffer("select count(1) from project ");
        List<Object> vlaues = appendWhere(sql, model);
        return JDBCUtil.queryInt(sql.toString(), vlaues);
    }
}
